package com.haiyi.software.app.basic;

import java.util.HashMap;
import java.util.Map;

import org.apache.http.Header;

import android.os.AsyncTask;
import android.os.Bundle;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.haiyi.software.app.MainApplication;
import com.haiyi.software.app.R;
import com.haiyi.software.tools.L;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

public abstract class ActivityBasicSplash extends ActivityBasic {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO 自动生成的方法存根
		super.onCreate(savedInstanceState);
	}

	private Boolean isWork = false;

	private Boolean isWorking = false;

	protected abstract void StartApp();

	protected synchronized void ckVersion() {

		if (!isWork) {

			isWork = true;

			Map<String, Object> ags = new HashMap<String, Object>();

			Map<String, Object> argsBody = new HashMap<String, Object>();

			argsBody.put("version", MainApplication.VERSION);

			argsBody.put("modelNo", "android");

			ags.put("body", argsBody);

			Gson gson = new Gson();

			String s = gson.toJson(ags);

			requestNetworkData(s);
		}
	}

	@Override
	protected void requestNetworkData(Object... args) {

		AsyncHttpClient client = MainApplication.getInstance()
				.getAsyncHttpClient();

		String strUrl = MainApplication.getInstance().getResources()
				.getString(R.string.URL)
				+ "chkversion.php";

		L.d(strUrl);

		RequestParams params = new RequestParams();

		params.put("args", args[0].toString());

		client.post(strUrl, params, responseHandler);

	}

	protected synchronized void getAddress() {

		if (!isWorking) {

			isWorking = true;

			AddressAsyncTask aat = new AddressAsyncTask();

			aat.execute();
		}

	}

	private AsyncHttpResponseHandler responseHandler = new AsyncHttpResponseHandler() {

		@Override
		public void onSuccess(int statusCode, Header[] headers,
				byte[] responseBody) {

			isWork = false;

			try {

				String str_jsonReturn = new String(responseBody, "UTF-8");

				L.d(str_jsonReturn);

				JsonParser parser = new JsonParser();

				JsonObject obj_return = parser.parse(str_jsonReturn)
						.getAsJsonObject();

				L.d(obj_return.toString());

				requestFinish(obj_return);

			} catch (Exception e) {
				// TODO 自动生成的 catch 块
				L.i("error=" + e.getMessage());
				requestError();
			}

		}

		@Override
		public void onFailure(int statusCode, Header[] headers,
				byte[] responseBody, Throwable error) {

			L.i("error=" + error.getMessage());

			isWork = false;

			requestError();

		}
	};

	protected class AddressAsyncTask extends
			AsyncTask<Integer, Integer, String> {

		String str_addres = "";
		java.net.InetAddress address;

		@Override
		protected String doInBackground(Integer... params) {

			try {

				String domain = MainApplication.getInstance().getResources()
						.getString(R.string.ServerIPS);

				address = java.net.InetAddress.getByName(domain);

			} catch (Exception e) {

				e.printStackTrace();
			}

			return null;
		}

		@Override
		protected void onPostExecute(String result) {

			super.onPostExecute(result);

			isWorking = false;

			str_addres = address.getHostAddress();

			L.i("address=" + str_addres);

			if (str_addres.length() > 0) {

				MainApplication.getInstance().setServerIP(str_addres);

				StartApp();

			} else {

				requestError();

			}

		}

	}

}
